home *** CD-ROM | disk | FTP | other *** search
- // recvsyx: record data from midi input
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include <ctype.h>
- #include <time.h>
- #include "sb.hpp"
- #include "filei3.hpp"
-
- char filename[128] = "recv.syx";
- Soundcard* card = 0;
- FILE* f = 0;
-
- int hex = 0;
- int sysex_only = 0;
- int echo = 0;
-
- unsigned char buf[32000];
- int buflen = 0;
- long skip = 0;
-
- void flushbuf()
- {
- if (buflen > 0)
- {
- if (hex)
- {
- static int col = 0;
-
- for (int i = 0; i < buflen; i++)
- {
- if (col >= 25 || (buf[i] >= 0x80 && buf[i] != 0xf8 && buf[i] != 0xfe && buf[i] != 0xf7))
- {
- fputc('\r', f);
- fputc('\n', f);
- col = 0;
- }
- fprintf(f, "%02X ", buf[i]);
- col++;
- }
- }
- else
- {
- if (fwrite(buf, buflen, 1, f) != 1)
- fprintf(stderr, "write error\n");
- }
- }
-
- buflen = 0;
- }
-
- void add(FILE* f, unsigned char* s, int len)
- {
- if (buflen + 7 > sizeof(buf))
- flushbuf();
- memcpy(buf+buflen, s, len);
- if (len > 0)
- buflen += len;
- }
-
- int receive_syx()
- {
- card->reset();
- unsigned char c;
- int ret = 0;
-
- fprintf(stderr, "recording...\n");
- card->startinput();
-
- f = fopen(filename, "wb");
- if (!f)
- {
- perror(filename);
- return 0;
- }
- int online = 0;
- int insysex = 0;
-
- while (!kbhit() || getch() != 27)
- {
- int count = card->hear(&c, 1);
- if (count <= 0)
- continue;
- if (c == 0xf8 || c == 0xfe)
- {
- if (online == 0)
- fprintf(stderr,"*\b");
- else if (online == 12)
- fprintf(stderr,".\b");
- online++;
- if (online == 24)
- online = 0;
- }
- if (insysex)
- {
- if (c == 0xf7)
- {
- insysex = 0;
- }
- }
- else
- {
- if (c == 0xf0)
- {
- insysex = 1;
- }
- else if (sysex_only)
- continue;
- }
-
- add(f, &c, 1);
- if (echo)
- {
- printf(" %02X", c);
- if (c == 0xF7)
- putchar('\n');
- }
- ret = 1;
- }
- if (echo)
- printf("\n");
- card->stopinput();
- if (f)
- {
- flushbuf();
- fclose(f); f = 0;
- }
- return ret;
- }
-
- int main(int argc, char** argv)
- {
- argc--; argv++;
- while (argc > 0 && **argv == '-')
- {
- if (strnicmp(*argv, "-hex", 4) == 0)
- {
- hex = 1;
- argc--; argv++;
- continue;
- }
- if (strnicmp(*argv, "-h", 2) == 0)
- {
- printf("usage: recvsyx [-h] [-hex] [-echo] [-sysex] [filename]\n");
- printf("-hex\tsave received data to text file (default: binary file)\n");
- printf("-echo\techo the received data (hexadecimal)\n");
- printf("-sysex\tsave only sysex messages (F0 ... F7)\n");
- printf("filename\tsave data to filename (default: recv.syx)\n");
- return 1;
- }
- if (strnicmp(*argv, "-echo", 2) == 0)
- {
- echo = 1;
- argc--; argv++;
- continue;
- }
- if (strnicmp(*argv, "-sysex", 2) == 0)
- {
- sysex_only = 1;
- argc--; argv++;
- continue;
- }
- fprintf(stderr, "invalid option %s\n", *argv);
- argc--; argv++;
- }
- if (argc > 0)
- {
- strcpy(filename, *argv);
- }
- card = detect_soundcard();
- if (!card)
- {
- fprintf(stderr, "Could not detect soundcard\n");
- return 1;
- }
-
- if (!receive_syx())
- fprintf(stderr, "no response from soundcard\n");
- else
- fprintf(stderr, "data written to file %s\n", filename);
- delete card;
- return 0;
- }
-